package com.bbs.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
//import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.bbs.beans.Submit;

import im.beans.User;
import manager.PersistentManager;

/**
 * 查看我发的帖子
 * Servlet implementation class MyTieZi
 */
//@WebServlet("/MyTieZi")
public class MyTieZi extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MyTieZi() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		String data = request.getParameter("data");
		//Upload upload = JSON.parseObject(data,Upload.class);
	    Upload upload=new Upload();
	    upload.setUserId(data);
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		Session session = sf.openSession();

		Transaction tx = session.beginTransaction();
		
		try{
			Query q = session.createQuery("from Submit s where s.user = ? order by s.changeTime desc");
			User tempU = new User();
			tempU.setId(Long.parseLong(upload.getUserId()));
			q.setParameter(0,tempU);
			q.setFirstResult(0);
			q.setMaxResults(10);
			download.setTies(q.list());
			
			tx.commit();
			download.setStatus(DownloadInfo.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		}
		finally {
			session.close();
		}
		
		response.setCharacterEncoding("UTF-8");
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	// 上行数据
	public static class Upload
	{
		// 用户帐号
		String userName;
		// 用户id
		String userId;
		public String getUserName() {
			return userName;
		}
		public void setUserName(String userName) {
			this.userName = userName;
		}
		public String getUserId() {
			return userId;
		}
		public void setUserId(String userId) {
			this.userId = userId;
		}
		
		
	}
	
	// 下行数据
	public static class Download
	{
		// 状态标记
		int status;
		
		// 我的所有帖子
		List<Submit> ties = new ArrayList<Submit>();

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

		public List<Submit> getTies() {
			return ties;
		}

		public void setTies(List<Submit> ties) {
			this.ties = ties;
		}
		
		
	}
	
}
